package com.fyzk.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnBasketPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 大小分adapter
 *
 * @author fxs
 */

public class JCLQDXFPayAdapter extends JCLQZhiFuBaseAdapter {

    private static final String TAG = "JC_ZhiFuAdapter";

    private int index;
    private boolean isdanguan;
    private List<JCLQMatchData> infors;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;

    private int color_bai;
    private int color_hei;
    private int color_gray;
    private int color_zong;
    private OnBasketPayTeamDeleteListener listener;

    public JCLQDXFPayAdapter(Context context, boolean type,
                             List<JCLQMatchData> infors, Handler handler, OnBasketPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        color_bai = Color.WHITE;
        color_hei = context.getResources().getColor(R.color.deep_black);
        color_gray = context.getResources().getColor(R.color.color_666666);
        color_zong = context.getResources().getColor(R.color.zc_bf_left);
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= infors.size()) {
            return null;
        }
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (position >= infors.size()) {
            return 0;
        }
        return position;
    }

    int co = 0;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jclq_pay_item_dxf, null);
            holder = new ViewHolder();
            holder.zhu = convertView
                    .findViewById(R.id.jclq_zhifu_list_item_zhu);
            holder.week_num = convertView
                    .findViewById(R.id.week_num);
            holder.delete = convertView
                    .findViewById(R.id.jclq_zhifu_list_item_delete);
            holder.sheng1 = convertView
                    .findViewById(R.id.jclq_zhifu_list_item_tv_sheng1);
            holder.fu1 = convertView
                    .findViewById(R.id.jclq_zhifu_list_item_tv_fu1);
            holder.da = convertView
                    .findViewById(R.id.jclq_child_dxf_dafen);
            holder.xiao = convertView
                    .findViewById(R.id.jclq_child_dxf_xiaofen);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCLQMatchData infor = infors.get(position);
        holder.zhu.setVisibility(View.VISIBLE);
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor
                .getMatchNum()));
        holder.sheng1.setText(infor.getHomeTeam());
        holder.fu1.setText(infor.getAwayTeam());
        holder.da.setText("大分 " + infor.getDxf_1());
        holder.xiao.setText("小分 " + infor.getDxf_2());
        holder.da.setClickable(false);
        holder.xiao.setClickable(false);

        JCLQMatchData.SPInfo sp_d = infor.splInfos.get(infor.DXF_1);
        JCLQMatchData.SPInfo sp_x = infor.splInfos.get(infor.DXF_2);

        holder.da.setChecked(sp_d.isSel);
        holder.xiao.setChecked(sp_x.isSel);
        holder.delete.setTag(R.string.jc_info, infor);
        holder.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                JCLQMatchData infor = (JCLQMatchData) v.getTag(R.string.jc_info);
                for (JCLQMatchData.SPInfo pl : infor.plInfos) {
                    pl.isSel = false;
                }
                infor.clickNum = 0;
                infors.remove(infor);
                notifyDataSetChanged();
                if (listener != null){
                    listener.onDelete(infor);
                }
            }
        });
        jiSuanZhuShu();
        return convertView;
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCLQMatchData> getInfor(List<JCLQMatchData> infors) {
//		List<JCLQMatchData> list = new ArrayList<JCLQMatchData>();
//		for (int i = 0; i < jcInfors.size(); i++) {
//			JCLQMatchData info = jcInfors.get(i);
//			int onclikNum = 0;
//			if (info.daFen_isChecked) {
//				onclikNum++;
//			}
//			if (info.xiaoFen_isChecked) {
//				onclikNum++;
//			}
//			info.setOnClickNum(onclikNum);
//
//			if (info.getOnClickNum() != 0) {
//				list.add(info);
//				Log.e(TAG, "选中：" + i);
//			}
//		}
        return infors;
    }

    /**
     * 计算注数（待修改）
     */
    public void jiSuanZhuShu() {

        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        for (int i = 0; i < infors.size(); i++) {
            JCLQMatchData info = infors.get(i);
//			info.btnHGPojo.get(JCLQMatchData.BIG_SCORE).isSel = info.plInfos.get(16).isSel;
//			info.btnHGPojo.get(JCLQMatchData.SMALL_SCORE).isSel = info.plInfos.get(17).isSel;
            if (info.clickNum != 0) {
                changCi++;
                if (info.clickNum != 1) {
                    isDanShi = false;
                }
            }
            clicknums.add(info.clickNum);
            dannums.add(false);// 暂不支持胆拖
        }

        List<Integer> chuans = new ArrayList<Integer>();

        // JC_GuoGuanDialog.setResult1(changCi);
        if (isdanguan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }

        } else {
            String chuan = JC_GuoGuanDialog.result;

            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }
                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);
                String betcode = JZBetContentUtil.getContentLQDXF(isdanguan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);
            }
        }
        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);

    }

    public List<JCLQMatchData> getInfors() {
        return infors;
    }

    public void setJLInfors(List<JCLQMatchData> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        CheckBox da;
        CheckBox xiao;
        LinearLayout zhu;
        LinearLayout delete;
        TextView sheng1;
        TextView fu1;
        TextView week_num;

    }

}
